package com.jqb.jsplab.tag;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class ListTag extends SimpleTagSupport {
	private String id;
	private String name;
	private String scope;

	@Override
	public void doTag() throws JspException, IOException {
		Object value = null;
		if ("page".equals(this.scope)) {
			value = super.getJspContext().getAttribute(this.name,
					PageContext.PAGE_SCOPE);
		}
		if ("request".equals(this.scope)) {
			value = super.getJspContext().getAttribute(this.name,
					PageContext.REQUEST_SCOPE);
		}
		if ("session".equals(this.scope)) {
			value = super.getJspContext().getAttribute(this.name,
					PageContext.SESSION_SCOPE);
		}
		if ("application".equals(this.scope)) {
			value = super.getJspContext().getAttribute(this.name,
					PageContext.APPLICATION_SCOPE);
		}
		
		if (value != null && value instanceof List<?>) {
			Iterator<?> itr = ((List<?>) value).iterator();
			while (itr.hasNext()) {
				super.getJspContext().setAttribute(this.id, itr.next());
				/**执行标签体null表示不输出*/
				super.getJspBody().invoke(null);
			}
		}
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getScope() {
		return scope;
	}

	public void setScope(String scope) {
		this.scope = scope;
	}

}
